package com.ie.controller.before;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.ie.comment.http.ResponseResult;
import com.ie.comment.sercurity.anno.AuthIgrone;

import com.ie.comment.sercurity.utiles.ConfigrarionBean;
import com.ie.comment.sercurity.utiles.RedisUtil;
import com.ie.entity.BUserEntity;
import com.ie.sevice.before.BUserService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

/**
 * @author: 申劭明
 * @date: 2019/10/30 10:28
 * @description: 前台用户控制器
 */
@RestController
@RequestMapping("api/before/user")
public class BUserController {

   @Autowired
    private BUserService bUserService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ConfigrarionBean config;

    @AuthIgrone
    @PostMapping("/register")
   public ResponseResult<Map<String , String>> register(@RequestBody BUserEntity bUserEntity){
      return bUserService.register(bUserEntity);
   }

    @AuthIgrone
    @PostMapping("/getcode")
    public void getcode(HttpServletResponse response) throws IOException {
        //hutool工具实现，定义图形验证码的长、宽、验证码字符数、干扰元素个数
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(116, 30, 4, 10);
        //将验证码存入redis
        redisUtil.set("code", circleCaptcha.getCode(), config.getRedisExpiration());
        //获取HTTP响应的输出流
        ServletOutputStream outputStream = response.getOutputStream();
        //将生成的验证码图片写入到输出流中，从而发送到客户端浏览器
        circleCaptcha.write(outputStream);
        //关闭输出流，释放资源
        outputStream.close();
    }

    @AuthIgrone
    @PostMapping("/login")
    public ResponseResult<Map<String , String>> login(@RequestBody BUserEntity bUserEntity){
        return bUserService.login(bUserEntity);
    }
}
